package com.ali.gts.servlet.filter;

import com.ali.gts.servlet.util.JDBCUtils;

import javax.servlet.*;
import java.io.IOException;

/**
 *
 *  使用统一的Filter 给 Servlet 添加try catch
 *  因为filterChain.doFilter 会间接的调用我们的Servlet 中的方法
 *
 */
public class TransactionFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        try {
            filterChain.doFilter(servletRequest,servletResponse);
            JDBCUtils.commitAndClose();
        } catch (Exception e) {
            e.printStackTrace();
            JDBCUtils.robackAndClose();
        }
    }

    @Override
    public void destroy() {

    }
}
